from database.db_handler import MysqlHander
from common.my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
from common.market_data import MarketData
import glob
import jieba
import re
import threading
import hashlib
import json
from database.db_business import DbBusiness
from common.entity_data import EntityData

'''
宏观数据：发展改革委员会
'''

class DongfangcaifuGaoguan:
    def __init__(self):
        self.entity = EntityData()
        self.business = DbBusiness()
        self.code_ex = {}
        self.code_name = {}
    
    def get_url(self, code, exchange_id):
        ex = ""
        if exchange_id == 1:
            ex = "SH"
        elif exchange_id == 2:
            ex = "SZ"
        elif exchange_id == 3:
            #ex = "BJ"
            return ""
        else:
            return ""
        return "http://emweb.securities.eastmoney.com/PC_HSF10/CompanyManagement/PageAjax?code=" + ex + code
    
    def get_online_data(self, url, code):
        proxy = self.business.query_proxy()
        json_data = json.loads(MyHttp.http_json_data_v2(url, proxy))
        gaoguan = ""
        for d in json_data["gglb"]:
            gaoguan = gaoguan + "|" + d["PERSON_NAME"]
        self.entity.del_entity_data(self.code_name[code] + "高管")
        self.entity.add_entity_data(self.code_name[code].replace(" ","") + "高管", "", 200, 2000002, self.code_name[code].replace(" ",""), "", gaoguan)
        self.entity.update_entity_data(self.code_name[code].replace(" ","") + "高管", "", 200, 2000002, self.code_name[code].replace(" ",""), "", gaoguan)

    
    def get_data(self):
        self.code_ex = self.business.query_stock_code_exchange()
        self.code_name = self.business.query_stock_code_name()
        for k,v in self.code_ex.items():
            url = self.get_url(k, v)
            if url == "":
                continue
            i = 0
            while i < 5:
                try:
                    self.get_online_data(url, k)
                    break
                except Exception as e:
                    i = i + 1


def dongfangcaifu_gaoguan_api():
    d = DongfangcaifuGaoguan()
    d.get_data()

if __name__ == '__main__':
    dongfangcaifu_gaoguan_api()
